<template>
	<view class="content">
		<view class="content-c">
			<image class="load-img" src="/static/images/common/loading.gif" mode=""></image>
			<view class="load-text color-9">信息加载中.....</view>
		</view>
	</view>
</template>

<script>
import { requestErrorTip } from '@/common/tools.js';
	
export default {
	data() {
		return {
			type: '',
			openid: '',
			orderId: '',
			state: ''
		};
	},
	onLoad(options) {
		requestErrorTip('model', JSON.stringify(options));
		this.orderId = options.order_id;
		this.money = Number(options.money);
		this.type = Number(options.type);
		this.uid = Number(options.uid);
		this.state = this.getQueryString('state');
		this.getCode();
	},
	methods: {
		getQueryString(name, url) {
			var url = url || window.location.href
			var reg = new RegExp('(^|&|/?)' + name + '=([^&|/?]*)(&|/?|$)', 'i')
			var r = url.substr(1).match(reg)
			if (r != null) {
				return r[2]
			}
			return null
		},
		getCode() {
			var code = this.getQueryString('code');
			code && this.getOpenId(code);
		},
		getOpenId(code) {
			let data = {
				code: code,
				scope: 2,
				state: this.state
			};
			// 公众号授权获取openid
			this.$Requset({
				url: this.$Api['account']['officiallogin'],
				data: data,
				method: 'post',
				success: (res) => {
					requestErrorTip('model', JSON.stringify(res) + 'getOpenId');
					if (res.status) {
						this.openid = res.data.openid;
						this.toPayHandler('wechatpay');
					} else {
						requestErrorTip('toast', res.msg);
					}
				},
				fail: (err) => {
					console.log(err);
				}
			})
			//模拟接口
			// this.$api.getOpenId(data, res => {
			// 	if (res.status) {
			// 		this.openid = res.data.openid;
			// 		this.toPayHandler('wechatpay');
			// 	} else {
			// 		requestErrorTip('toast', res.msg);
			// 	}
			// });
		},
		checkWXJSBridge(data) {
			let that = this;
			requestErrorTip('model', JSON.stringify(data) + 'checkWXJSBridge');
			let interval = setInterval(() => {
				if (typeof window.WeixinJSBridge != 'undefined') {
					clearTimeout(interval);
					that.onBridgeReady(data);
				}
			}, 200);
		},
		onBridgeReady(data) {
			var _this = this;
			window.WeixinJSBridge.invoke(
				'getBrandWCPayRequest',
				{
					appId: data.appid, // 公众号名称，由商户传入
					timeStamp: data.timeStamp, // 时间戳，自1970年以来的秒数
					nonceStr: data.nonceStr, // 随机串
					package: data.package,
					signType: data.signType, // 微信签名方式：
					paySign: data.paySign // 微信签名
				},
				function(res) {
					if (res.err_msg === 'get_brand_wcpay_request:ok') {
						requestErrorTip('toast', '支付成功');
					} else if (res.err_msg === 'get_brand_wcpay_request:cancel') {
						requestErrorTip('toast', '取消支付');
					} else {
						requestErrorTip('toast', '支付失败');
					}
					setTimeout(() => {
						uni.navigateTo({
							url: '/pages/order/pay-success?payment_type=' + this.type + '&order_id=' + this.orderId
						});
					}, 1000);
				}
			);
		},
		toPayHandler(code) {
			let data = {
				payment_code: code,
				payment_type: this.type
			};
			data['ids'] = (this.type == 1 || this.type == 5 || this.type == 6) ? this.orderId : this.uid
			//data['ids'] = this.type == 1 ? this.orderId : this.uid

			if (this.type == 1 && this.orderId) {
				// 微信jsapi支付
				if (this.openid) {
					data['params'] = {
						trade_type: 'JSAPI_OFFICIAL',
						openid: this.openid
					};
				}
			} else if (this.type == 2 && this.money) {
				if (this.openid) {
					data['params'] = {
						trade_type: 'JSAPI_OFFICIAL',
						money: this.money,
						openid: this.openid
					};
				}
			} else if ((this.type == 5 || this.type == 6) && this.orderId) {
				if (this.openid) {
					data['params'] = {
						trade_type: 'JSAPI_OFFICIAL',
						openid: this.openid,
						formid: this.orderId
					};
				}
			}
			this.$Requset({
				url: this.$Api['account']['pay'],
				data: data,
				method: 'post',
				success: (res) => {
					requestErrorTip('model', JSON.stringify(res) + 'pay');
					if (res.status) {
						const data = res.data;
						this.checkWXJSBridge(data);
					} else {
						requestErrorTip('toast', res.msg);
					}
					// requestErrorTip('model', JSON.stringify(res) + 'getOpenId');
					// if (res.status) {
					// 	this.openid = res.data.openid;
					// 	this.toPayHandler('wechatpay');
					// } else {
					// 	requestErrorTip('toast', res.msg);
					// }
				},
				fail: (err) => {
					console.log(err);
				}
			})
			// this.$api.pay(data, res => {
			// 	requestErrorTip('model', JSON.stringify(res) + 'pay');
			// 	if (res.status) {
			// 		const data = res.data;
			// 		this.checkWXJSBridge(data);
			// 	} else {
			// 		this.$common.errorToShow(res.msg);
			// 	}
			// });
		}
	}
};
</script>

<style>
.content {
	position: relative;
	height: 80vh;
}
.content-c {
	position: absolute;
	top: 50%;
	left: 50%;
	transform: translate(-50%, -50%);
	text-align: center;
}
.load-img {
	width: 100upx;
	height: 100upx;
}
.load-text {
	font-size: 26upx;
}
</style>
